﻿// <copyright file="Form1.cs" company="None">(c) Guy Smith-Ferrier, 2012</copyright>

namespace AcceptLanguageSetter
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Text;
    using System.Windows.Forms;
    using Browser;

    /// <summary>
    /// Form1 is the AcceptLanguageSetter main form
    /// </summary>
    public partial class Form1 : Form
    {
        /// <summary>
        /// Gets or sets the IBrowserLanguagePreference
        /// </summary>
        private IBrowserLanguagePreference browserLanguagePreference;

        /// <summary>
        /// Initializes a new instance of the Form1 class
        /// </summary>
        public Form1()
        {
            this.InitializeComponent();

            this.rbInternetExplorer.Enabled = new IEBrowserLanguagePreference().BrowserExists;
            this.rbFirefox.Enabled = new FirefoxBrowserLanguagePreference().BrowserExists;
            this.rbChrome.Enabled = new ChromeBrowserLanguagePreference().BrowserExists;

            string defaultBrowserCommand = DefaultBrowser.Command;
            if (this.rbInternetExplorer.Enabled && defaultBrowserCommand.IndexOf("iexplore.exe", 0, StringComparison.InvariantCultureIgnoreCase) != -1)
            {
                this.rbInternetExplorer.Checked = true;
            }
            else if (this.rbFirefox.Enabled && defaultBrowserCommand.IndexOf("firefox.exe", 0, StringComparison.InvariantCultureIgnoreCase) != -1)
            {
                this.rbFirefox.Checked = true;
            }
            else if (this.rbChrome.Enabled && defaultBrowserCommand.IndexOf("chrome.exe", 0, StringComparison.InvariantCultureIgnoreCase) != -1)
            {
                this.rbChrome.Checked = true;
            }
        }

        /// <summary>
        /// LoadLanguagePreference loads the browser's language preference and updates the listbox with the language preferences
        /// </summary>
        private void LoadLanguagePreference()
        {
            this.listBoxLanguages.Items.Clear();
            string languages = this.browserLanguagePreference.LanguagePreferences;
            foreach (string language in languages.Split(','))
            {
                this.listBoxLanguages.Items.Add(language);
            }
        }

        /// <summary>
        /// The event handler for the Set button's Click event
        /// </summary>
        /// <param name="sender">The object that sent the event</param>
        /// <param name="e">The EventArgs</param>
        [SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "This is autogenerated by Visual Studio")]
        private void buttonSet_Click(object sender, EventArgs e)
        {
            this.browserLanguagePreference.LanguagePreferences = this.GetLanguageString();
        }

        /// <summary>
        /// The event handler for the Add button's Click event
        /// </summary>
        /// <param name="sender">The object that sent the event</param>
        /// <param name="e">The EventArgs</param>
        [SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "This is autogenerated by Visual Studio")]
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (this.textBoxNewLanguage.Text != string.Empty)
            {
                this.listBoxLanguages.Items.Add(this.textBoxNewLanguage.Text);
            }
        }

        /// <summary>
        /// The event handler for the Delete button's Click event
        /// </summary>
        /// <param name="sender">The object that sent the event</param>
        /// <param name="e">The EventArgs</param>
        [SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "This is autogenerated by Visual Studio")]
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (this.listBoxLanguages.SelectedIndex != -1)
            {
                this.listBoxLanguages.Items.RemoveAt(this.listBoxLanguages.SelectedIndex);
            }
        }

        /// <summary>
        /// The event handler for the Move Up button's Click event
        /// </summary>
        /// <param name="sender">The object that sent the event</param>
        /// <param name="e">The EventArgs</param>
        [SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "This is autogenerated by Visual Studio")]
        private void buttonMoveUp_Click(object sender, EventArgs e)
        {
            if (this.listBoxLanguages.SelectedIndex > 0)
            {
                int index = this.listBoxLanguages.SelectedIndex;
                string value = this.listBoxLanguages.Items[index].ToString();
                this.listBoxLanguages.Items.RemoveAt(index);
                this.listBoxLanguages.Items.Insert(index - 1, value);
                this.listBoxLanguages.SelectedIndex = index - 1;
            }
        }

        /// <summary>
        /// The event handler for the Move Down button's Click event
        /// </summary>
        /// <param name="sender">The object that sent the event</param>
        /// <param name="e">The EventArgs</param>
        [SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "This is autogenerated by Visual Studio")]
        private void buttonMoveDown_Click(object sender, EventArgs e)
        {
            if (this.listBoxLanguages.SelectedIndex > -1 && this.listBoxLanguages.SelectedIndex < this.listBoxLanguages.Items.Count - 1)
            {
                int index = this.listBoxLanguages.SelectedIndex;
                string value = this.listBoxLanguages.Items[index].ToString();
                this.listBoxLanguages.Items.RemoveAt(index);
                this.listBoxLanguages.Items.Insert(index + 1, value);
                this.listBoxLanguages.SelectedIndex = index + 1;
            }
        }

        /// <summary>
        /// Get's the language string from the selection in the user interface
        /// </summary>
        /// <returns>The language string from the selection in the user interface</returns>
        private string GetLanguageString()
        {
            if (this.listBoxLanguages.Items.Count == 0)
            {
                return string.Empty;
            }

            StringBuilder builder = new StringBuilder(this.listBoxLanguages.Items[0].ToString());
            for (int itemNumber = 1; itemNumber < this.listBoxLanguages.Items.Count; itemNumber++)
            {
                builder.Append("," + this.listBoxLanguages.Items[itemNumber].ToString());
            }

            return builder.ToString();
        }

        /// <summary>
        /// The event handler for the Internet Explorer radio button's Click event
        /// </summary>
        /// <param name="sender">The object that sent the event</param>
        /// <param name="e">The EventArgs</param>
        [SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "This is autogenerated by Visual Studio")]
        private void rbInternetExplorer_CheckedChanged(object sender, EventArgs e)
        {
            this.browserLanguagePreference = new IEBrowserLanguagePreference();
            this.LoadLanguagePreference();
        }

        /// <summary>
        /// The event handler for the Firefox radio button's Click event
        /// </summary>
        /// <param name="sender">The object that sent the event</param>
        /// <param name="e">The EventArgs</param>
        [SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "This is autogenerated by Visual Studio")]
        private void rbFirefox_CheckedChanged(object sender, EventArgs e)
        {
            this.browserLanguagePreference = new FirefoxBrowserLanguagePreference();
            this.LoadLanguagePreference();
        }

        /// <summary>
        /// The event handler for the Chrome radio button's Click event
        /// </summary>
        /// <param name="sender">The object that sent the event</param>
        /// <param name="e">The EventArgs</param>
        [SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "This is autogenerated by Visual Studio")]
        private void rbChrome_CheckedChanged(object sender, EventArgs e)
        {
            this.browserLanguagePreference = new ChromeBrowserLanguagePreference();
            this.LoadLanguagePreference();
        }

        /// <summary>
        /// The event handler for the Languages listbox's Double Click event
        /// </summary>
        /// <param name="sender">The object that sent the event</param>
        /// <param name="e">The EventArgs</param>
        [SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "This is autogenerated by Visual Studio")]
        private void listBoxLanguages_DoubleClick(object sender, EventArgs e)
        {
            if (this.listBoxLanguages.SelectedIndex > -1)
            {
                int index = this.listBoxLanguages.SelectedIndex;
                string value = this.listBoxLanguages.Items[index].ToString();
                this.listBoxLanguages.Items.RemoveAt(index);
                this.listBoxLanguages.Items.Insert(0, value);
                this.listBoxLanguages.SelectedIndex = 0;

                this.browserLanguagePreference.LanguagePreferences = this.GetLanguageString();
            }
        }
    }
}
